package com.wandian.view.components.ui.avatar.view.localpic
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.ui.*;
	import com.wandian.view.components.ui.avatar.model.*;
    import com.wandian.view.components.ui.avatar.view.*;
    import com.wandian.view.components.ui.avatar.view.avatar.*;
    import com.wandian.view.components.ui.avatar.view.ui.*;
    import com.wandian.view.components.ui.avatar.SK_CursorMove;
    import com.wandian.view.components.ui.avatar.SK_CursorScale;
    import com.wandian.view.components.ui.avatar.SK_Loading;
    import com.wandian.view.components.ui.avatar.SK_Tip;
    import com.wandian.view.components.ui.avatar.SK_TurnLeft;
    import com.wandian.view.components.ui.avatar.SK_TurnRight;

    public class LocalPicArea extends Sprite
    {
		public var tip:MovieClip;

		private var swfStage:Stage;

		private var cutBox:CutBox;
		private var cutBox_minW:Number = 10;
        private var cutBox_minH:Number = 10;

        private var TurnLeft:SK_TurnLeft;
		private var TurnRight:SK_TurnRight;
        private var cutAreaBg:RectBox;

        private var flagCurX:Number;
        private var flagCurY:Number;
        private var flagVX:Number;
        private var flagVY:Number;

        private var picRate:Number;

        private var _cursorScale:SK_CursorScale;
        private var _sourceBMD:BitmapData;
        private var _cursorMove:SK_CursorMove;


        private var _picX:Number;
        private var _picY:Number;
        private var _picContainer:Sprite;
        private var _avatar:AvatarArea;

        public var loaddingUI:MovieClip;

        public function LocalPicArea()
        {

            this.init();
            return;
        }
		// 控件初始化
        private function init() : void
        {
            this.cutAreaBg = new RectBox(Param.pSize[0], Param.pSize[1]);
            this._picContainer = new Sprite();
            this._cursorMove = new SK_CursorMove();
            this._cursorMove.mouseEnabled = this._cursorMove.visible = false;
            this._cursorScale = new SK_CursorScale();
            this._cursorScale.mouseEnabled = this._cursorScale.visible = false;

            this.tip = new SK_Tip() as MovieClip;
            this.tip.y = this.tip.x = 1;
            this.tip.stop();
            this.tip.mouseEnabled = false;

			 //左右旋转
            this.TurnLeft = new SK_TurnLeft();
			this.TurnRight = new SK_TurnRight();
            this.TurnLeft.y = this.TurnRight.y = Param.pSize[1] + 12;
            this.TurnRight.x = 220;
			this.TurnLeft.addEventListener(MouseEvent.CLICK, this.rotationPicLeft);
            this.TurnRight.addEventListener(MouseEvent.CLICK, this.rotationPicRight);
            this.showTunBtns(false);

            this.createLoaddingUI();
            addChild(this.cutAreaBg);
            addChild(this.tip);
            addChild(this._picContainer);
            addChild(this.TurnLeft);
            addChild(this.TurnRight);
            addChild(this.loaddingUI);
            this.addEventListener(Event.ADDED_TO_STAGE, this.addedToStage);
            return;
        }

        private function resetCursor(event:MouseEvent) : void
        {
            Mouse.show();
            this._cursorMove.visible = this._cursorScale.visible = false;
            this.cutBox.removeEventListener(MouseEvent.ROLL_OUT, this.resetCursor);
            this.cutBox.removeEventListener(MouseEvent.MOUSE_MOVE, this.moveCursor);
            this.cutBox.removeEventListener(MouseEvent.MOUSE_DOWN, this.changeCutBox);
            return;
        }

		// 左旋转/右旋转  按钮状态
        public function showTunBtns(s:Boolean) : void
        {
            this.TurnRight.visible = this.TurnLeft.visible = s;
            return;
        }

        private function moveCursor(event:MouseEvent) : void
        {
            var _loc_3:Sprite = null;
            var tgt = event.target as Sprite;
            switch(tgt)
            {
                case this.cutBox.cutArea:
                {
                    _loc_3 = this._cursorMove as Sprite;
                    break;
                }
                case this.cutBox.flag:
                {
                    _loc_3 = this._cursorScale as Sprite;
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.cursorFollow(_loc_3);
            event.updateAfterEvent();
            return;
        }

        private function createLoaddingUI() : void
        {
            this.loaddingUI = new SK_Loading() as MovieClip;
            this.loaddingUI.x = Math.ceil(Param.pSize[0] / 2);
            this.loaddingUI.y = Math.ceil(Param.pSize[1] / 2) - 20;
            this.loaddingUI.stop();
            this.loaddingUI.visible = false;
            return;
        }



		// 缩略结束
        private function stopResizeCutBox(event:MouseEvent) : void
        {
            Mouse.show();
            this._cursorScale.visible = false;
            this.cutBox.addEventListener(MouseEvent.MOUSE_OVER, this.changeCursor);
            this.swfStage.removeEventListener(Event.ENTER_FRAME, this.resizeCutBox);
            this.swfStage.removeEventListener(MouseEvent.MOUSE_UP, this.stopResizeCutBox);
            return;
        }

        private function addedToStage(event:Event) : void
        {
            this.swfStage = this.stage;
            var _cutview = this.parent as CutView;
            this._avatar = _cutview.avatarArea;
            this.removeEventListener(Event.ADDED_TO_STAGE, this.addedToStage);
            return;
        }

        private function changeCursor(event:MouseEvent) : void
        {
            var tgt = event.target as Sprite;
            if (tgt == this.cutBox.cutArea)
            {
                Mouse.hide();
                this._cursorMove.visible = true;
                this._cursorScale.visible = false;
                this._cursorMove.x = this.mouseX;
                this._cursorMove.y = this.mouseY;
            }
            else if (tgt == this.cutBox.flag)
            {
                Mouse.hide();
                this._cursorScale.visible = true;
                this._cursorMove.visible = false;
                this._cursorScale.x = this.mouseX;
                this._cursorScale.y = this.mouseY;
            }
            this.cutBox.addEventListener(MouseEvent.MOUSE_MOVE, this.moveCursor);
            this.cutBox.addEventListener(MouseEvent.MOUSE_DOWN, this.changeCutBox);
            this.cutBox.addEventListener(MouseEvent.MOUSE_OUT, this.resetCursor);
            return;
        }

        private function cursorFollow(param1:Sprite) : void
        {
            param1.x = this.mouseX;
            param1.y = this.mouseY;
            return;
        }

		// 创建 剪切选择框
        public function addCutBox(pic:Bitmap) : void
        {
            var _w:Number = NaN;
            var _h:Number = NaN;
            if (this.cutBox != null)
            {
                removeChild(this.cutBox);
                this.cutBox = null;
            }
            if (pic.width < Param.pSize[2] || pic.height < Param.pSize[3])
            {	// 原版 _w = pic.width > pic.height ? (pic.height) : (pic.width);
				if(pic.width/pic.height >= Param.pSize[2]/Param.pSize[3]){
					_h = pic.height;
                    _w = _h * Param.pSize[2] / Param.pSize[3];
				}
				else{
					_w = pic.width;
                    _h = _w * Param.pSize[3] / Param.pSize[2];
				}
            }
            else
            {
                _w = Param.pSize[2];
                _h = Param.pSize[3];
            }
            this.cutBox = new CutBox(_w, _h);
            this.cutBox.x = this._picContainer.x + (this._picContainer.width - this.cutBox.cutArea.width) * 0.5;
            this.cutBox.y = this._picContainer.y + (this._picContainer.height - this.cutBox.cutArea.height) * 0.5;
			addChild(this.cutBox);
            addChild(this._cursorMove);
            addChild(this._cursorScale);
            pic.mask = this.cutBox.maskArea;
            this.cutBox.addEventListener(MouseEvent.MOUSE_OVER, this.changeCursor);
            var bmd = this.getAvatarBMD(this._picX, this._picY, this.picRate, this.cutBox);
            this._avatar.changeAvatars(bmd);
            return;
        }


        public function setLocalPicSize(bmd:BitmapData) : void
        {
            var _pic:Bitmap = null;

            if (this.tip != null)
            {
                removeChild(this.tip);
                this.tip = null;
            }
            this._sourceBMD = bmd;
            var _sourcePic = new Bitmap(bmd);
            var _widthRate = Param.pSize[0] / _sourcePic.width;
            var _heightRate = Param.pSize[1] / _sourcePic.height;
            this.picRate = _widthRate < _heightRate ? (_widthRate) : (_heightRate); //缩略时取最小长宽比
            _sourcePic.width = _sourcePic.width * this.picRate;
            _sourcePic.scaleY = _sourcePic.scaleX;

            while (this._picContainer.numChildren)
            {
                _pic = this._picContainer.removeChildAt((this._picContainer.numChildren - 1)) as Bitmap;
                _pic = null;
            }

            this._sourceBMD = _sourcePic.bitmapData;
            var albumPic = new Bitmap(_sourcePic.bitmapData);		// 原图缩略
				albumPic.width = _sourcePic.width;
				albumPic.scaleY = albumPic.scaleX;

            _sourcePic.alpha = 0.5;
			this._picContainer.addChild(_sourcePic);
            this._picContainer.addChild(albumPic);

            this._picX = this._picContainer.x = (Param.pSize[0] - _sourcePic.width) * 0.5 + 1;
            this._picY = this._picContainer.y = (Param.pSize[1] - _sourcePic.height) * 0.5 + 1;

            this.addCutBox(albumPic);
            this.showTunBtns(true);
            return;
        }

/* **************** 左右旋转 **************** */
		// 左旋转
        private function rotationPicLeft(event:MouseEvent) : void
        {
            this._sourceBMD = this.changeBmdLeft(this._sourceBMD);
            this.setLocalPicSize(this._sourceBMD);
            return;
        }
        private function changeBmdLeft(bmd:BitmapData) : BitmapData
        {
            var _w = bmd.width;
            var _h = bmd.height;
            var newBmd = new BitmapData(_h, _w, false);
            var _tmp = new Matrix();
            _tmp.rotate((-Math.PI) * 0.5);
            _tmp.translate(0, _w);
            newBmd.draw(bmd, _tmp);
            return newBmd;
        }
		// 右旋转
        private function rotationPicRight(event:MouseEvent) : void
        {
            this._sourceBMD = this.changeBmdRight(this._sourceBMD);
            this.setLocalPicSize(this._sourceBMD);
            return;
        }
        private function changeBmdRight(bmd:BitmapData) : BitmapData
        {
            var _w = bmd.width;
            var _h = bmd.height;
            var newBmd = new BitmapData(_h, _w, false);
            var _tmp = new Matrix();
            _tmp.rotate(Math.PI * 0.5);
            _tmp.translate(_h, 0);
            newBmd.draw(bmd, _tmp);
            return newBmd;
        }

/* **************** 剪切框动作 **************** */

        private function getAvatarBMD(_x:Number, _y:Number, picRate:Number, cutbox:CutBox) : BitmapData
        {
            var _w = cutbox.maskArea.width / picRate;
            var _h = cutbox.maskArea.height / picRate;
            var _bmd = new BitmapData(_w, _h, false);
            var _RectangleX = int((cutbox.x - _x) / picRate) + 0.5;
            var _RectangleY = int((cutbox.y - _y) / picRate) + 0.5;
            _bmd.copyPixels(this._sourceBMD, new Rectangle(_RectangleX, _RectangleY, _w, _h), new Point(0, 0));
            return _bmd;
        }
		// 拖动
        private function moveCutBox(event:MouseEvent) : void
        {
            this.cutBox.startDrag(false, new Rectangle(this._picContainer.x, this._picContainer.y, this._picContainer.width - this.cutBox.maskArea.width, this._picContainer.height - this.cutBox.maskArea.height));
			var _loc_2 = this.getAvatarBMD(this._picX, this._picY, this.picRate, this.cutBox);
            this._avatar.changeAvatars(_loc_2);
            return;
        }
		// 结束拖动
        private function stopMoveCutBox(event:Event) : void
        {
            this.cutBox.stopDrag();
            this.swfStage.removeEventListener(MouseEvent.MOUSE_MOVE, this.moveCutBox);
            this.swfStage.removeEventListener(MouseEvent.MOUSE_UP, this.stopMoveCutBox);
            return;
        }
		// 改变大小
        private function changeCutBox(event:MouseEvent) : void
        {
            var tgt = event.target as Sprite;
            switch(tgt)
            {
                case this.cutBox.cutArea:
                {
                    this.swfStage.addEventListener(MouseEvent.MOUSE_MOVE, this.moveCutBox);
                    this.swfStage.addEventListener(MouseEvent.MOUSE_UP, this.stopMoveCutBox);
                    break;
                }
                case this.cutBox.flag:
                {
                    this.flagVY = this.mouseY - this.cutBox.y - this.cutBox.maskArea.height;
                    this.flagVX = this.mouseX - this.cutBox.x - this.cutBox.maskArea.width;
                    this.flagCurX = this.mouseX;
                    this.flagCurY = this.mouseY;
                    this.cutBox.removeEventListener(MouseEvent.MOUSE_OVER, this.changeCursor);
                    this.swfStage.addEventListener(Event.ENTER_FRAME, this.resizeCutBox);
                    this.swfStage.addEventListener(MouseEvent.MOUSE_UP, this.stopResizeCutBox);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }
        private function resizeCutBox(event:Event) : void
        {
            var _currW:Number = NaN;
            var _currH:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var bmd:BitmapData = null;
            Mouse.hide();
            this._cursorScale.x = this.mouseX;
            this._cursorScale.y = this.mouseY;
            this._cursorScale.visible = true;
            if (this.mouseX != this.flagCurX || this.mouseY != this.flagCurY)
            {
                _currW = this.mouseX - this.cutBox.x - this.flagVX;
                _currH = this.mouseY - this.cutBox.y - this.flagVY;
				if (_currH > this._picContainer.y + this._picContainer.height - this.cutBox.y)
                {
                    _currH = this._picContainer.y + this._picContainer.height - this.cutBox.y;
                }
                if (_currH < this.cutBox_minH)
                {
                    _currH = this.cutBox_minH;
                }
                if (_currW > this._picContainer.x + this._picContainer.width - this.cutBox.x)
                {
                    _currW = this._picContainer.x + this._picContainer.width - this.cutBox.x;
                }
                if (_currW < this.cutBox_minW)
                {
                    _currW = this.cutBox_minW;
                }
                _loc_4 = _currW - this.cutBox.maskArea.width;
                _loc_5 = _currH - this.cutBox.maskArea.height;
				/* if(_loc_4/_loc_5 >= Param.pSize[2]/Param.pSize[3]){
					this.cutBox.maskArea.height = _currH;
                    this.cutBox.maskArea.width = _currH * Param.pSize[2] / Param.pSize[3];
				}
				else{
					this.cutBox.maskArea.width = _currW;
                    this.cutBox.maskArea.height = _currW * Param.pSize[3] / Param.pSize[2];
				}*/
                /* 原版固定1:1比例*/
				if (_loc_4 > _loc_5)
                {
                    this.cutBox.maskArea.height = _currH;
                    this.cutBox.maskArea.width = _currH;
                }
                else
                {
                    this.cutBox.maskArea.height = _currW;
                    this.cutBox.maskArea.width = _currW;
                }
                this.cutBox.cutArea.width = this.cutBox.maskArea.width;
                this.cutBox.cutArea.height = this.cutBox.maskArea.height;
                this.cutBox.flag.x = this.cutBox.maskArea.width;
                this.cutBox.flag.y = this.cutBox.maskArea.height;
                bmd = this.getAvatarBMD(this._picX, this._picY, this.picRate, this.cutBox);
                this._avatar.changeAvatars(bmd);
            }
            return;
        }

    }
}
